package com.liush.srb.core.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liush.srb.base.result.ResponseVo;
import com.liush.srb.core.pojo.entity.UserInfo;
import com.liush.srb.core.pojo.vo.UserInfoSearchVO;
import com.liush.srb.core.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: liush
 * @CreateTime: 2023-04-03  18:16
 * @Description: 会员管理模块
 */
@RestController
//@CrossOrigin
@Api(tags = "会员管理模块")
@RequestMapping("/admin/core/userInfo")
public class AdminUserInfoController {

    @Resource
    UserInfoService userInfoService;

    @ApiOperation("条件分页查询用户列表")
    @GetMapping("list/{pageNum}/{pageSize}")
    public ResponseVo list(UserInfoSearchVO userInfoSearchVO,
                           @PathVariable("pageNum") Integer pageNum,
                           @PathVariable("pageSize") Integer pageSize) {
        Page<UserInfo> page = new Page<>(pageNum, pageSize);
        userInfoService.getUserInfos(userInfoSearchVO, page);
        return ResponseVo.ok().data("page", page);
    }

    @ApiOperation("锁定/解锁会员")
    @PutMapping("{userId}/{status}")
    public ResponseVo updateUserStatus(@PathVariable("userId") Long userId,
                                       @PathVariable("status") Integer status) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        userInfo.setStatus(status);
        // 根据id更新：id值必须存在， 其他属性如果值不为null更新  null值忽略
        // update userinfo set status = xxx where id = userId;
        userInfoService.updateById(userInfo); //配置了自动填充时  更新可以实现自动填充
//        userInfoService.update(Wrappers.lambdaUpdate(UserInfo.class)
//            .eq(UserInfo::getId,userId)
//            .set(UserInfo::getStatus , status));//配置了自动填充时  更新不会自动填充
        return ResponseVo.ok().message(status == 0 ? "锁定成功" : "解锁成功");
    }
}
